var commonCityData = require('../../utils/city.js')
let network = require('../../utils/network.js')
var observer = require('../../libs/observer').observer;
var action = require('../../libs/mobx').action;



var addrItem = null ;
//获取应用实例
var app = getApp()
Page({
  props: {
    addrMgrStore: require('../../store/addressMgrStore.js').default
  },
  // 是否是编辑
  edit :false,
  data: {
    addressArr: [],
    multiIndex: [0, 0, 0],
    districtAddress:'请选择'
  },
  
  bindCancel: function () {
    wx.navigateBack({})
  },

  bindSave: function (e) {
    var that = this;
    var linkMan = e.detail.value.linkMan;
    var address = e.detail.value.address;
    var mobile = e.detail.value.mobile;
    var code = e.detail.value.code;

    if (linkMan == "") {
      wx.showToast({
        title: '请填写联系人姓名',
      });
      return
    }
    if (mobile == "") {
      wx.showToast({
        title: '请填写手机号码',
      });
      return
    }
    if (this.data.districtAddress == "请选择") {
      wx.showToast({
        title: '请选择地址',
      });
      return
    }
    if (address == "") {
      wx.showToast({
        title: '请填写详细地址',
      });
      return
    }
    let actionName = this.edit ? 'edit' : 'create';
    network.requestLoading('mp/client/deliveryAddress/' + actionName, {
      phoneNum: mobile,
      fullAddress: this.data.districtAddress + address,
      recievName: linkMan,
      detailAddress: address,
      districtAddress: this.data.districtAddress,
      isDefault: 0,
      id: addrItem ? addrItem.id : ''
    }, '正在处理...', function () {
      wx.navigateBack({
      })
    }, function (err) {
      wx.showToast({
        title: err.data.message,
      })
    })
  },

  

  bindPickerChange: function (event){

    let addressData = this.data.addressData;
    this.setData({

    })
    this._getDistrictAddress()
  },

  bindPickerColumnChange:function(event){
    console.log(event)
    var data = {
      addressArr: this.data.addressArr,
      multiIndex: this.data.multiIndex
    };
    switch (event.detail.column){
      case 0:
        data.multiIndex = [event.detail.value,0,0] ;
        break;
      case 1:
        data.multiIndex[1] = event.detail.value;
        data.multiIndex[2] = 0;
        break;
        data.multiIndex[2] = event.detail.value;
      case 2:
        break;
    }
    data.multiIndex[event.detail.column] = event.detail.value;
    data.addressArr = commonCityData.getCityArr(data.multiIndex)
    this.setData(data)
    
  
  },

  _getDistrictAddress:function(){
    let data = {
      addressArr: this.data.addressArr,
      multiIndex: this.data.multiIndex
    };
    let myDistrictAddress = data.addressArr[0][data.multiIndex[0]].name +        (data.addressArr[1].length > 0 ? data.addressArr[1][data.multiIndex[1]].name : "") + (data.addressArr[2].length > 0 ? data.addressArr[2][data.multiIndex[2]].name : "")
    this.setData({
      districtAddress: myDistrictAddress
    })
    return myDistrictAddress;
  },
  // 根据详细地址重新计算addressData
  _resetAddressArr: function (provinceName, cityName, countyName){
    let data = {
      addressArr: this.data.addressArr,
      multiIndex: this.data.multiIndex
    };

    data.addressArr[0].map((value,index)=>{
      if(value.name === provinceName){
        data.multiIndex[0]=index
      }
    })
    data.addressArr = commonCityData.getCityArr(data.multiIndex)

    data.multiIndex[1]=0;
    data.addressArr[1].map((value, index) => {
      if (value.name === cityName) {
        data.multiIndex[1] = index
      }
    })
    data.addressArr = commonCityData.getCityArr(data.multiIndex)

    data.multiIndex[2] = 0;
    data.addressArr[2].map((value, index) => {
      if (value.name === countyName) {
        data.multiIndex[2] = index
      }
    })
    data.addressArr = commonCityData.getCityArr(data.multiIndex)

    this.setData(data)
     

  },

  onLoad: function (options) {
    var that = this;
    console.log(options)
    
    let itemid = options.itemid
    let tempAddressArr = commonCityData.getCityArr(this.data.multiIndex)
    that.setData({
      addressArr: tempAddressArr
    })
    if (itemid) {
      this.edit = true ;
      addrItem = this.props.addrMgrStore.findAddById(itemid);
      console.log(addrItem)
      that.setData(
        {
          districtAddress: addrItem.districtAddress === "" ? "请选择" : addrItem.districtAddress,
          addressData: {
            linkMan: addrItem.recievName,
            mobile: addrItem.phoneNum,
            address: addrItem.detailAddress,
          },
          
        }
      );
    }
  },

  selectCity: function () {
  //  let arr =  commonCityData.getCityArr()
    console.log(this.data.addressArr)
  },

  readFromWx: function () {
    let that = this;
    wx.chooseAddress({
      success: function (res) {
        console.log(res)
        let provinceName = res.provinceName;
        let cityName = res.cityName;
        let diatrictName = res.countyName;
        let retSelIdx = 0;
        that.setData({
          districtAddress: provinceName + cityName + diatrictName
        })
        that._resetAddressArr(provinceName, cityName, diatrictName)
        that.setData({
          wxaddress: res,
        });
      }
    })
  }
})
